<template>
  <div class="package__item">
    <span class="hot__tag" v-if="isHot"></span>
    <div class="package__content"
      :class="{ 'is-active': activeItemId === item.id, 'small': !showVipDiscount }"
      @click="$emit('select', item)"
      >
      <div class="time__content fs14">
        {{ item.name}}
      </div>
      <div class="price-wrap">
        <div class="real-price fs24">
          <span class="fs16">￥</span>
          <em>{{  finalPrice | moneyToyuan }}</em>
        </div>
        <div class="origin-price">
          ￥{{ item.origin_price|moneyToyuan }}
        </div>
      </div>
      <div class="nums-text">{{ numsText }}</div>
      <div class="duration-text">- {{ durationText }} -</div>
      <div class="mem-discount" v-if="vipDiscountPrice">会员折扣价：{{ vipDiscountPrice | moneyToyuan }}</div>
    </div>
  </div>
</template>

<script>
const limitOptions = new Map([
  [1440, '1天'],
  [4320, '3天'],
  [10080, '7天'],
  [21600, '15天'],
  [43200, '1个月'],
  [129600, '3个月'],
  [259200, '半年'],
  [525600, '1年'],
  [1051200, '2年'],
  [1576800, '3年'],
  [36792000, '永久']
])
export default {
  name: '',
  props: {
    item: {
      type: Object,
      default: () => {}
    },
    userInfo: {
      type: Object,
      default: () => {}
    },
    activeItemId: {
      type: Number,
      default: 0
    },
    showVipDiscount: Boolean
  },
  data () {
    return {
    }
  },
  computed: {
    finalPrice () {
      const actByProduct = this.item['10004']?.[0]
      if (actByProduct) {
        return this.userInfo.has_buy
          ? Math.min(actByProduct.old_user_price, this.item.price)
          : Math.min(actByProduct.new_user_price, this.item.price)
      }
      return this.item.price
    },
    durationText () {
      return limitOptions.get(this.item.bus_ext.duration) + '有效'
    },
    numsText () {
      const price = this.item.price / 100
      const count = this.item.related_item?.items?.[0]?.setting?.count
      let n = Number((price / count * 1000).toFixed(2))
      if (n) {
        return `${n}元 / 千能量`
      }
      n = Number((price / count * 10000).toFixed(2))
      return `${n}元 / 万能量`
    },
    isHot () {
      return false
    },
    vipDiscountPrice () {
      if (this.showVipDiscount) {
        const discount = this.item['vip-discount']?.[0]
        if (discount?.setting) {
          return Math.max(discount?.setting.price, 1)
        }
      }
      return null
    }
  },
  filters: {
    moneyToyuan (val = 0) {
      return parseFloat((val / 100).toFixed(2))
    },
    valToFixed2 (val = 0) {
      return (val / 100).toFixed(2)
    }
  }
}
</script>

<style lang="scss" scoped>
.package__item{
  padding-top: 5px;
}
.package__content {
  width: 100%;
  height: 180px;
  // flex: 0 0 190px;
  border-radius: 10px;
  text-align: center;
  color: #fff;
  background-color: rgba($color: #fff, $alpha: 0.05);
  box-sizing: border-box;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  &.small {
    height: 160px;
  }
  .time__content {
    background-color: rgba($color: #fff, $alpha: 0.1);
    font-weight: bold;
    line-height: 36px;
  }
  .price-wrap {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 12px;
    gap: 6px;
  }
  .real-price {
    font-size: 24px;
    white-space: nowrap;
    span {
      margin-right: 2px;
      font-size: 16px;
      padding-bottom: 4px;
    }
    em {
      font-weight: bold;
    }
  }
  .origin-price {
    font-size: 12px;
    opacity: .5;
    text-decoration: line-through;
  }
  .nums-text {
    display: inline-block;
    margin: 6px auto 0 auto;
    background: rgba(255,255,255,0.1);
    border-radius: 20px;
    font-weight: 400;
    font-size: 12px;
    line-height: 16px;
    text-align: center;
    padding: 5px 12px;

  }
  .duration-text {
    margin: 10px auto 0 auto;
    font-weight: 400;
    font-size: 12px;
  }
  .mem-discount{
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 33px;
    background: rgba(203,136,255,0.1);
    font-weight: bold;
    font-size: 12px;
    color: #CB88FF;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
.package__content:hover {
  background-color: rgba($color: #fff, $alpha: 0.1);
}
.package__content.is-active {
  background: linear-gradient( 180deg, rgba(85, 38, 187, .3) 0%, rgba(196, 70, 255, .3) 100%);
  .time__content {
    background: rgba(255,255,255,0.1);
  }
  .mem-discount {
    background: linear-gradient( 180deg, #9C60FF 0%, #C360FF 100%);
    color: #fff;
  }
}
.hot__tag {
  position: absolute;
  top: 2px;
  right: -4px;
  display: block;
  width: 54px;
  height: 54px;
  background: url(~@/assets/hot-tag.png) no-repeat center center / 100% 100%;
  color:#fff;
  span {
    display: block;
    transform: rotate(45deg);
    white-space: nowrap;
    position: relative;
    top: 11px;
    left: 9px;
    font-size: 12px;
  }
}
</style>
